<?php	
	class the_model_object extends the_model_class {
		function get_object($query = "") {			
			if (!$this->test()) return oh_this();			
						
			$args 		= $this->get_args($query,"form","class:form");
			$args_info 	= $this->get_args_info($args,"object");
			$prefix 	= $this->get_prefix();				
			if ($query) {
				$j = array();					
				$w = array();
				$f = array("DISTINCT o.id","o.*");
				$m = 0;
				$limit 		= "";
				$orderby 	= "";
				foreach($args as $key=>$value) {
					if (false) {
					} else if ($key == "orderby") {
						$orderby_data 	= $this->eat_orderby($key,$value);
						$orderby 		= $orderby_data['sql'];
						if ($orderby_data['use_meta']) {
							$j[] = "JOIN {$prefix}meta AS m{$m} ON o.id=m{$m}.meta_object";
							$f[] = "m{$m}.meta_value AS ".$orderby_data['meta'];
							$w[] = "m{$m}.meta_key='".$orderby_data['meta']."'";
							$m++;
						} else {
							$j = "";
							$f = "";
						}
					} else if ($key == "limit") {
						$limit = "LIMIT ".$value; 
					} else {
						if ($args_info[$key]['is_vald']) {
							if ($args_info[$key]['is_meta']) {
								$j[] = "JOIN {$prefix}meta AS m{$m} ON o.id=m{$m}.meta_object";
								$f[] = "m{$m}.meta_value AS ".oh_underscore($args_info[$key]['meta']);
								$w[] = "m{$m}.meta_key='".$args_info[$key]['meta']."'";
								$w[] = "m{$m}.meta_value LIKE '".$value."'";
								//if ()								
								$m++;
							} else {
								$w[] = "$key LIKE '$value'";				
							}
						}
					}
				}
				$w = "WHERE ".implode(" AND ",$w);
				$f = implode(",",$f);
				$j = implode(" ",$j);
			} else {
				$j = "";
				$w = "";
				$f = "";
			}			
			$q = "SELECT $f FROM {$prefix}object AS o $j $w $orderby $limit";
			if ($args->get == "query") {
				return $q;
			} else {
				if ($args->get == "count") {
					$r = oh($this->datasource)->get_results($q);
					return count($r);
				} else {				
					$r = oh($this->datasource)->get_results($q);
					return $this->set_decoration($r);
				}
			}
		}
				
		function set_object($query = "", $data = NULL, $otherdata = NULL) {
			global $oh_object_model;
			if (!$this->test_installation()) {
				return $oh_object_model;
			}
			if (is_string($data)&&is_string($otherdata)) {
				$a = $this->get_args($query,"form");
				$w = $this->get_where($a);
				$c = $this->get_class($a->form);
				$p = $this->get_prefix();
				if (in_array($data,$this->model_object_fields["real"])) {
					$s = "SET $data='$otherdata'";					
					$q = "UPDATE {$p}object $s $w";
					$e = oh($this->datasource)->query($q);
					oh($this->datasource)->error();
					return $e;
				} else {
					oh_message("Field not recognized: <b>$data</b>");
				}					
			}
			// 			
			if ($data) {
				$args 	= $this->get_args($query,"form");
				$data	= $this->set_casting($args,$data);
				if ($otherdata) {
				} else {
					$s = $this->get_set($data);
				}
				$prefix = $this->get_prefix();				
				$w		= $this->get_where($args);
								
				$q 		= "UPDATE {$prefix}object $s $w";
				$e		= oh($this->datasource)->query($q);
				oh($this->datasource)->error();
				return $e;
			} else {
				$oh_object_model->selector = "object::".$query;
			}
			return $oh_object_model;			
		}
		function del_object($query = "") {			
			global $oh_object_model;			
			if (!$this->test_installation()) {				
				return $oh_object_model;
			}
			$args 	= $this->get_args($query,"form");
			$prefix = $this->get_prefix();				

			if ($query) {
				$w = array();
				$limit = "";
				$orderby = "";
				foreach($args as $key=>$value) {
					if (false) {
					} else if ($key == "id") {
						$w[] = "$key = '$value'";				
					} else {
						$w[] = "$key LIKE '$value'";				
					}
				}
				$w = "WHERE ".implode(" AND ",$w);
			} else {
				$w = "";
			}				
			$q = "DELETE FROM {$prefix}object $w";
			return oh($this->datasource)->query($q);
		}
		
		// Create new object depending imponed class
		function new_object($query = "") {
			if (!$this->test()) return oh_this();
			
			$a 	= $this->get_args($query,"form");
			$c 	= $this->get_class($a->form);
			//$f 	= $this->filter_args($a);
			$id = $this->new_raw_object($a);

			foreach($c->fields["meta"] as $meta_key) {
				$meta_value = isset($args->$meta_key) ? ($args->$meta_key) : "";
				$this->new_meta($id,$meta_key,$meta_value);
			}				
			foreach($c->fields["chil"] as $type=>$name) {
				$chil_id = $this->new_raw_object("$type&boss=$id");
			}
			return $id;
		}
		
		function hit_object($query = "") {
			if (!$this->test()) return false;

			$args 	= $this->get_args($query,"form");
			$prefix = $this->get_prefix();
			
			$w = array();
			foreach($args as $key=>$value) {
				$w[] = "$key='$value'";
			}
			$w = implode(" AND ",$w);			
			$q = "SELECT id FROM {$prefix}object WHERE $w LIMIT 1";								
			if ($args->get == "query") {
				return $q;
			}
			$c = oh($this->datasource)->get_results($q);
			return count($c) > 0;
		}
		
		function new_raw_object($query) {
			$args 	= $this->get_args($query,"form");
			$prefix = $this->get_prefix();
			$args->born = date("Y-m-d H:i:s");
			$i		= $this->get_object_insert($args);
			$fields = $i["fields"];
			$values = $i["values"];
			$q = "INSERT INTO {$prefix}object($fields) VALUES ($values)";
			oh($this->datasource)->query($q);
			oh($this->datasource)->error();					
			if ($this->datasource == "mysql") {
				$id = mysql_insert_ID();
			}				
			return $id;		
		}
	}
?>


